
package com.husong.common.captcha;

import javax.servlet.http.HttpServletRequest;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;

/**
 * @author MaxMin.
 */

public class JCaptcha {
    public static final ManageableImageCaptchaService captchaService
            = new ManageableImageCaptchaService(new FastHashMapCaptchaStore(),
            new CaptchaEngine(), 180, 100000, 75000);

    public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse) {
        if (request.getSession(false) == null) return false;
        boolean validated = false;
        try {
            String id = request.getSession().getId();
            if (id!=null && !"".equals(id)){
                validated = captchaService.validateResponseForID(id, userCaptchaResponse).booleanValue();
            }
        } catch (CaptchaServiceException e) {
            e.printStackTrace();
        }
//        LoggerUtils.info(JCaptcha.class.getName(), "is validate:"+validated);
        return validated;
    }

    public static boolean hasCaptcha(HttpServletRequest request, String userCaptchaResponse) {
        if (request.getSession(false) == null) return false;
        boolean validated = false;
        try {
            String id = request.getSession().getId();
            validated = captchaService.hasCapcha(id, userCaptchaResponse);
        } catch (CaptchaServiceException e) {
            e.printStackTrace();
        }
        return validated;
    }
}
